<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>loadGlb</title>
    <script src="./threejs/build/three.min.js"></script>
    <script src="./threejs/examples/js/loaders/GLTFLoader.js"></script>
    <script src="./threejs/examples/js/controls/OrbitControls.js"></script>
    <script src="./threejs/examples/js/libs/stats.min.js"></script>
    <script src="./threejs/examples/js/libs/dat.gui.min.js"></script>
</head>
<body onload="draw();">
    <script>
        
    var renderer;
    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias:true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        //告诉渲染器需要阴影效果
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        // 设置背景色
        renderer.setClearColor(15.0,4.0,2.0,2.0);
        document.body.appendChild(renderer.domElement);
    }

    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 100000);
        camera.position.set(30000, 5000, 5000);
        camera.lookAt(new THREE.Vector3(0,0,0));
    }

    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    //初始化dat.GUI简化试验流程
    var gui;
    function initGui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
        };
        var datGui = new dat.GUI();
        //将设置属性添加到gui当中，gui.add(对象，属性，最小值，最大值）
    }

    var light;
    function initLight() {
        // 白光，灯光强度为1；
        scene.add(new THREE.AmbientLight(0x404040, 2));

        light = new THREE.PointLight(0xffffff);
        light.position.set(-20,-20,10);

        //告诉平行光需要开启阴影投射
        light.castShadow = true;

        scene.add(light);
    }

    function initModel() {

        //辅助工具
        // 三维坐标系
        var helper = new THREE.AxesHelper(50);
        scene.add(helper);

        var loader = new THREE.GLTFLoader();
        loader.load("/FactoryDemo/file/NewProject.gltf", function (gltf) {
            //创建纹理
            // var mat = new THREE.MeshLambertMaterial({color: 0x00ffff});
            // var mesh = new THREE.Mesh(gltf);
            // // 改变模型相对于X,Y,Z轴的位置
            // mesh.rotation.x = 0.8 * Math.PI; // 改变模型位置
            // mesh.rotation.y = -0.3 * Math.PI; // 改变模型位置
            // mesh.scale.set(0.3, 0.3, 0.3); //缩放
            // geometry.center(); //居中显示
            // mesh.castShadow=true;

            // gltf.scene.traverse( function ( child ) {
            // 	if ( child.isMesh ) {
            //         child.material.emissive =  child.material.color;
			// 		child.material.emissiveMap = child.material.map;
            // 	}
            // } );
            
            gltf.scene.scale.set(0.001,0.001,0.001);
            gltf.scene.rotation.x=-0.5 * Math.PI;
            gltf.scene.rotation.z=-1 * Math.PI;
            scene.add(gltf.scene);
            console.log(gltf)
        });


        // // 底部平面
        // var planeGeometry = new THREE.PlaneGeometry(100, 200);
        // var planeMaterial = new THREE.MeshStandardMaterial({color:0xaaaaaa});

        // var plane = new THREE.Mesh(planeGeometry, planeMaterial);
        // plane.rotation.x = - 0.5 * Math.PI;
        // plane.position.y = -17;

        // //告诉底部平面需要接收阴影
        // plane.receiveShadow = true;

        // scene.add(plane);
    }

    //初始化性能插件
    var stats;
    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    //用户交互插件 鼠标左键按住旋转，右键按住平移，滚轮缩放
    var controls;
    function initControls() {

        controls = new THREE.OrbitControls( camera, renderer.domElement );

        // 如果使用animate方法时，将此函数删除
        // controls.addEventListener( 'change', render );
        // 使动画循环使用时阻尼或自转 意思是否有惯性
        controls.enableDamping = true;
        //动态阻尼系数 就是鼠标拖拽旋转灵敏度
        //controls.dampingFactor = 0.25;
        //是否可以缩放
        controls.enableZoom = true;
        //是否自动旋转
        controls.autoRotate = false;
        controls.autoRotateSpeed = 0.5;
        //设置相机距离原点的最远距离
        controls.minDistance  = 1;
        //设置相机距离原点的最远距离
        controls.maxDistance  = 200;
        //是否开启右键拖拽
        controls.enablePan = true;
    }

    function render() {

        renderer.render( scene, camera );
    }

    //窗口变动触发的函数
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        render();
        renderer.setSize( window.innerWidth, window.innerHeight );

    }

    function animate() {
        //更新控制器
        render();
        //更新性能插件
        stats.update();

        controls.update();

        requestAnimationFrame(animate);
    }

    function draw() {
        initGui();
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initControls();
        initStats();

        animate();
        window.onresize = onWindowResize;
    }

    </script>
    
</body>
</html>